package logic

import (
	"context"
	"fmt"
	"shoppings/model/mysql"

	"shoppings/shoprpc/internal/svc"
	"shoppings/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *shoprpc.AddRequest) (*shoprpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	product := mysql.Product{
		Number:   int(in.Number),
		Image:    in.Image,
		Name:     in.Name,
		Nav:      in.Nav,
		Label:    in.Label,
		Price:    float64(in.Price),
		ActPrice: float64(in.ActPrice),
		Stock:    int(in.Stock),
		Sales:    int(in.Sales),
		Type:     in.Type,
		UserId:   int(in.UserId),
	}
	mysql.DB.Debug().Where("name = ?", in.Name).Find(&product)
	if product.ID != 0 {
		return nil, fmt.Errorf("商品已存在")
	}
	mysql.DB.Debug().Create(&product)
	return &shoprpc.AddResponse{Id: int64(product.ID), UserId: int64(product.UserId)}, nil
}
